package luz.javaproxy.plugins.GuiLogger;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import java.util.Properties;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;

import luz.javaproxy.plugin.PluginInterface;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class GuiLogger extends JPanel implements PluginInterface {
	private static final long serialVersionUID = 250656714073344221L;
	private static final Log log = LogFactory.getLog(GuiLogger.class);
	private JScrollPane scrLog;
	private GuiLoggerTable tblLog;
	private JFormattedTextField txtLogLimit;
	private JLabel lblLogLimit;
	private JCheckBox cbAutoScroll;
	private JButton bntClear;
	private int logLimit=100;
	private boolean autoScroll;
	private String CONFIG_logLimit="logLimit";
	private String CONFIG_autoScroll="autoScroll";

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new GuiLogger());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public GuiLogger() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)this);
			this.setLayout(thisLayout);
			{
				scrLog = new JScrollPane();
				scrLog.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
				{
					tblLog = new GuiLoggerTable(logLimit);
					tblLog.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
					scrLog.setViewportView(tblLog);
				}
			}
			{
				bntClear = new JButton();
				bntClear.setText("Clear");
				bntClear.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						tblLog.clear();
					}
				});
			}
			{
				cbAutoScroll = new JCheckBox();
				cbAutoScroll.setText("AutoScroll");
				cbAutoScroll.setSelected(autoScroll);
				cbAutoScroll.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						autoScroll=cbAutoScroll.isSelected();
					}
				});
			}
			{
				NumberFormat numFormat = NumberFormat.getIntegerInstance();
				numFormat.setGroupingUsed(false);
				txtLogLimit = new JFormattedTextField(numFormat);
				txtLogLimit.setValue(logLimit);
				txtLogLimit.addPropertyChangeListener("value", new PropertyChangeListener(){
					public void propertyChange( PropertyChangeEvent evt) {
						logLimit=((Number)txtLogLimit.getValue()).intValue();
					}
				});
			}
			{
				lblLogLimit = new JLabel();
				lblLogLimit.setText("Log Limit:");
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(txtLogLimit, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(cbAutoScroll, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(lblLogLimit, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(bntClear, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(scrLog, 0, 211, Short.MAX_VALUE));
			thisLayout.setHorizontalGroup(thisLayout.createParallelGroup()
				.addComponent(scrLog, GroupLayout.Alignment.LEADING, 0, 387, Short.MAX_VALUE)
				.addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				    .addPreferredGap(scrLog, bntClear, LayoutStyle.ComponentPlacement.INDENT)
				    .addComponent(bntClear, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
				    .addGap(0, 115, Short.MAX_VALUE)
				    .addComponent(lblLogLimit, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				    .addComponent(txtLogLimit, GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)
				    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, GroupLayout.PREFERRED_SIZE)
				    .addComponent(cbAutoScroll, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addContainerGap()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public JPanel getPluginPanel() {
		return this;
	}
	
	@Override
	public Properties getSettings() {
		Properties p = new Properties();
		p.put(CONFIG_logLimit, logLimit);
		p.put(CONFIG_autoScroll, autoScroll);
		return p;
	}
	
	@Override
	public void setSettings(Properties config) {
		logLimit=Integer.parseInt(config.getProperty(CONFIG_logLimit, "100"));
		autoScroll=Boolean.valueOf(config.getProperty(CONFIG_autoScroll, "false"));
		txtLogLimit.setValue(logLimit);
		tblLog.setLimit(logLimit);
		cbAutoScroll.setSelected(autoScroll);
	}
	
	@Override
	public void onRequest(final HttpRequest request) {
		// Nothing		
	}
	
	@Override
	public void onResponse(final HttpRequest request, final HttpResponse response) {
		log.info("log packet");
		tblLog.addPackets(request, response);
		if (cbAutoScroll.isSelected()){
			//TODO serious scroll to end
			//scrLog.getVerticalScrollBar().setValue(tblLog.getHeight()-1);
			//tblLog.scrollRectToVisible(tblLog.getCellRect(tblLog.getRowCount()-1, tblLog.getColumnCount(), true));

			
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					scrLog.getVerticalScrollBar().setValue(scrLog.getVerticalScrollBar().getMaximum());
				}
			});
		}
	}



	
	



}
